package com.clss.webrtclibrary;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import com.clss.webrtclibrary.Lg;

import org.webrtc.EglBase;
import org.webrtc.MediaStream;
import org.webrtc.PeerConnectionFactory;
import org.webrtc.RendererCommon;
import org.webrtc.SurfaceViewRenderer;

import java.util.HashMap;

import static com.clss.webrtclibrary.KurentoServer.WEBRTC_TYPE_RECV;

/**
 * 只接收视频房间
 * @author liuv
 * @date 2018/5/8
 */

public class ReceiveVideoRoom {
    private static final String TAG = "ReceiveVideoRoom";

    private MediaInfoBean mMediaInfoBean;
    private Context mContext;
    private KurentoServer mKurentoServer;
    private KurentoMediaCallback mKurentoMediaCallback;
    private HashMap<String, MediaStream> mRemoteStreamMap = new HashMap<>();
    private EglBase mEglBase;
    private ProxyVideoSink mFullProxyVideoSink = new ProxyVideoSink();
    private SurfaceViewRenderer mFullRenderer;
    private IVideoRoomListener mIVideoRoomListener;

    public ReceiveVideoRoom(MediaInfoBean mMediaInfoBean, SurfaceViewRenderer fullRenderer,
                            IVideoRoomListener IVideoRoomListener) {
        this.mMediaInfoBean = mMediaInfoBean;
        this.mIVideoRoomListener = IVideoRoomListener;
        this.mContext = mIVideoRoomListener.getContext();
        this.mEglBase = EglBase.create();
        this.mFullRenderer = fullRenderer;

        mFullRenderer.init(mEglBase.getEglBaseContext(), null);
        mFullRenderer.setScalingType(RendererCommon.ScalingType.SCALE_ASPECT_FILL);
        mFullRenderer.setEnableHardwareScaler(true);

        mFullProxyVideoSink.setTarget(mFullRenderer);

        startVideo();
    }

    private void startVideo(){
        mKurentoServer = new KurentoServer(mContext, WEBRTC_TYPE_RECV,
                new KurentoGatewayCallback());
        PeerConnectionFactory.Options options = new PeerConnectionFactory.Options();
        mKurentoServer.initializeMediaContext(options);
    }

    public boolean stopVideo(){
        if (mFullProxyVideoSink != null) {
            mFullProxyVideoSink.setTarget(null);
        }

        if (mRemoteStreamMap.size() > 0) {
            Lg.i(TAG, "---mRemoteStreamMap.size() > 0===" + mRemoteStreamMap.size());
            mRemoteStreamMap.clear();
        }
        mFullProxyVideoSink.setTarget(null);
        if (mFullRenderer != null){
            mFullRenderer.release();
            mFullRenderer = null;
        }

        mFullProxyVideoSink = null;
//        mEglBase.releaseSurface();
        mEglBase.release();
        return mKurentoServer.closeAllLiveVideo();
    }

    private void videoStart(){
        if (mIVideoRoomListener != null){
            mIVideoRoomListener.videoOnStart();
        }
    }

    /** 收到新的信令 */
    public void receivedNewSign(KurentoSignallingBean receivedBean){
        if (mKurentoServer != null){
            mKurentoServer.receivedNewMessage(receivedBean);
        }
    }

    /**
     * 网关回调
     */
    private class KurentoGatewayCallback implements IKurentoGatewayCallback {

        @Override
        public MediaInfoBean getMediaInfo() {
            return mMediaInfoBean;
        }

        @Override
        public IKurentoMediaCallbacks getMediaHandleCallback() {
            return mKurentoMediaCallback == null ?
                    mKurentoMediaCallback = new KurentoMediaCallback()
                    : mKurentoMediaCallback;
        }

        @Override
        public EglBase getEglBase() {
            return mEglBase;
        }

        @Override
        public IVideoRoomListener getVideoRoomListener() {
            return mIVideoRoomListener;
        }

        @Override
        public void webSocketDisconnect() {
            Lg.i(TAG, "---webSocketDisconnect===");
            if (mIVideoRoomListener != null){
                mIVideoRoomListener.videoRoomDisconnect();
            }
        }

        @Override
        public void signalingParamError(String error) {
            if (mIVideoRoomListener != null){
                mIVideoRoomListener.videoError(error);
            }
        }

        @Override
        public void arrivedRoom(KurentoSignallingBean signallingBean) {

        }

        @Override
        public void leaveRoom(String id) {
            if (mIVideoRoomListener != null){
                mIVideoRoomListener.leaveRoom();
            }
        }

        @Override
        public void showConnectionInfo(String info) {

        }

        @Override
        public Boolean getVideoTrackEnable() {
            return true;
        }
    }

    /**
     * media回调
     */
    public class KurentoMediaCallback implements IKurentoMediaCallbacks {

        @Override
        public void success(KurentoPeerManager handle) {

        }

        @Override
        public void onLocalStream(MediaStream stream) {
            Lg.i(TAG, "---onLocalStream===");
        }

        @Override
        public void onRemoteStream(MediaStream stream, String id) {
            Lg.i(TAG, "---onRemoteStream===" + id);
            videoStart();
            if (TextUtils.isEmpty(id)){
                return;
            }
            mRemoteStreamMap.put(id, stream);
            stream.videoTracks.get(0).addSink(mFullProxyVideoSink);
        }

        @Override
        public void onRemoveRemoteStream(String id) {

        }

        @Override
        public void onDetached() {

        }

        @Override
        public Intent getScreenIntent() {
            return null;
        }

        @Override
        public void enableVideo(String id) {
            Lg.i(TAG, "---enableVideo===" + id);
        }

        @Override
        public void disableVideo(String id) {
            Lg.i(TAG, "---disableVideo===" + id);
        }
    }
}
